package parser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import opennlp.tools.sentdetect.SentenceDetector;
import opennlp.tools.tokenize.Tokenizer;
import opennlp.tools.util.Span;

public class OpenNLPAnnotation extends ParserAnnotation {
	private Tokenizer tok;
	private List<String> sents;
	private List<String[]> toks;
	private List<Integer[]> indexes;
	private OpenNLPPipeline p;

	public OpenNLPAnnotation(OpenNLPPipeline p, String document) {
		super(document);
		this.p = p;
	}

	@Override
	protected void parseImplementation() {
		SentenceDetector sd = p.getSentenceDetector();
		tok = p.getTokenizer();
		if (skipSentDetection)
			sents = Arrays.asList(document);
		else
			sents = Arrays.asList(sd.sentDetect(document));
		toks = new ArrayList<String[]>();
		indexes = new ArrayList<Integer[]>();
		for (String s : sents) {
			toks.add(tok.tokenize(s));
			Span[] sp = tok.tokenizePos(s);
			Integer[] index = new Integer[sp.length*2];
			for (int i = 0; i < sp.length; i++) {
				index[i*2] = sp[i].getStart();
				index[i*2+1] = sp[i].getEnd();
			}
			indexes.add(index);
		}
	}

	@Override
	public List<String> getSentences() {
		return sents;
	}

	@Override
	public int getNbrSentences() {
		return sents.size();
	}

	@Override
	public List<String> getTokens(int sent) {
		return Arrays.asList(toks.get(sent));
	}

	@Override
	public List<String> getPOS(int sent) {
		throw new Error("POS for OpenNLP is unimplemented");
	}

	@Override
	public List<String> getLemmas(int sent) {
		throw new Error("Lemmas for OpenNLP is unimplemented");
	}

	@Override
	public List<Integer> getTokenIndexes(int sent) {
		return Arrays.asList(indexes.get(sent));
	}
}
